/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Helper.DepartmentHelper;
import common.Constant;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author KIENLT
 */
public class DepartmentModel extends AbstractTableModel{

    ClassLoader c = this.getClass().getClassLoader();
    
    ArrayList<DepartmentHelper> DepartmentList;
    
    private final String colunmsName[] = { Constant.getBundle().getString("id"), Constant.getBundle().getString("departmentName") };
    private final Class columnsClass[] = { String.class, String.class };
    
    public DepartmentModel(ArrayList<DepartmentHelper> DepartmentList){
        this.DepartmentList = DepartmentList;
    }
    
    @Override
    public int getRowCount() {
        return DepartmentList.toArray().length;
    }

    @Override
    public int getColumnCount() {
        return colunmsName.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch(columnIndex){
            case 0 : 
                return DepartmentList.get(rowIndex).getDepartmentID();
            case 1 : 
                return DepartmentList.get(rowIndex).getDepartmentName();
            case 2 :
                return DepartmentList.get(rowIndex).isCheck();
            default: 
                return null;
        }
    }
    
    @Override
    public void setValueAt(Object val, int rowIndex, int columnIndex) {
//nếu edit tại column Boolean
        if (columnIndex == 2)
//thì lấy giá trị mới sau khi edit (val) gán vào data của model
            DepartmentList.get(rowIndex).setCheck(Boolean.parseBoolean(val.toString()));
        fireTableRowsUpdated(rowIndex, rowIndex);
    }
    
    @Override
    public String getColumnName(int icol){
        return colunmsName[icol];
    }
    
    @Override
    public Class getColumnClass(int icol){
        return columnsClass[icol];
    }
    
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return columnIndex == 2;
    }

  
     public void deleteRow(int deleteRow) {
        DepartmentList.remove(deleteRow);
        DepartmentList.trimToSize();
        fireTableRowsDeleted(deleteRow, deleteRow);
    }
}
